﻿using System;
using System.Collections.Specialized;
using System.Web;

namespace Eneta.Portal.Forum.Utility
{
    public class UrlQuery
    {
        // Fields
        private NameValueCollection _queryString;
        private readonly string _url;

        // Methods
        public UrlQuery()
        {
            _url = HttpContext.Current.Request.Url.AbsolutePath;
        }

        public UrlQuery(string value)
        {
            var index = value.IndexOf('?');
            if (index != -1)
            {
                _url = value.Substring(0, index);
                _queryString = NameValueCollection(value);
            }
            else
            {
                _url = value;
            }
        }

        public void FormToQuery(string param)
        {
            Set(param, HttpContext.Current.Request.Form[param]);
        }

        public string Get()
        {
            string str = "";
            if (this.QueryString.Count != 0)
            {
                str = "?";
                for (int i = 0; i <= (this.QueryString.Count - 1); i++)
                {
                    if (i != 0)
                    {
                        str = str + "&";
                    }
                    str = str + this.QueryString.GetKey(i) + "=" + this.QueryString.Get(i);
                }
            }
            return str;
        }

        public string Get(string param)
        {
            return this.QueryString[param];
        }

        public static NameValueCollection NameValueCollection(string qs)
        {
            NameValueCollection values = new NameValueCollection();
            qs = (qs.IndexOf('?') > 0) ? qs.Remove(0, qs.IndexOf('?') + 1) : qs;
            Array array = qs.Split("&".ToCharArray());
            for (int i = 0; i < array.Length; i++)
            {
                string[] strArray = array.GetValue(i).ToString().Split("=".ToCharArray());
                values.Add(strArray[0], strArray[1]);
            }
            return values;
        }

        public void Set(string param, string value)
        {
            if (param != string.Empty)
            {
                if ((value == string.Empty) || (value == null))
                {
                    this.QueryString.Remove(param);
                }
                else
                {
                    this.QueryString[param]= value;
                }
            }
        }

        // Properties
        public string AbsoluteUri
        {
            get
            {
                return (this.Url + this.Get());
            }
        }

        public string this[string param]
        {
            get
            {
                return this.Get(param);
            }
            set
            {
                this.Set(param, value);
            }
        }

        public NameValueCollection QueryString
        {
            get
            {
                if (this._queryString == null)
                {
                    this._queryString = new NameValueCollection(HttpContext.Current.Request.QueryString);
                }
                return this._queryString;
            }
        }

        public string Url
        {
            get
            {
                return this._url;
            }
        }

        public string VirtualFolder
        {
            get
            {
                return this.Url.Substring(0, this.Url.LastIndexOf("/") + 1);
            }
        }
    }
}
